//services/global.js"
import axios from 'axios';
import { Message, Loading } from 'element-ui'
let loadingInstance = null //这里是loading
// let ConfigBaseURL;
// if(process.env.NODE_ENV !='development'){
    // ConfigBaseURL = 'https://localhost:8080/api'
// }else{
    // ConfigBaseURL = 'http://192.16.16.37:8080/api/'
// }

// 初始化实例
const http = axios.create({
    timeout: 7000, // 请求超时时间
    // baseURL: ConfigBaseURL,
    // method: 'post',
    headers: {
        // "token":'9999',
        'Content-Type': 'application/x-www-form-urlencoded'
    }
});
var token='9999';
axios.defaults.headers.common["token"] = token;
http.dicUrl = 'http://192.168.16.37:8084/sysDicFeign/getDicItemList/';
// http.defaults.headers.common['Token'] = '9999'
// http.baseURL = url => { return ConfigBaseURL + url;}
// http.defaults.baseURL = process.evn.NODE_ENV === 'development' ? 'devUrl' : 'propUrl'
// 设置请求头
// http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
// 请求拦截器
http.interceptors.request.use((config) => { 
    loadingInstance = Loading.service({
        lock: true,
        text: 'loading...'
      })
    return config; 
}, (error) => { return Promise.reject(error); });
// 响应拦截器
http.interceptors.response.use((response) => { 
    loadingInstance.close()
    return response; 
}, (error) => { 
    console.log('TCL: error', error)
    const msg = error.Message !== undefined ? error.Message : ''
    Message({
        message: '网络错误' + msg,
        type: 'error',
        duration: 3 * 1000
    })
    loadingInstance.close()
    return Promise.reject(error); 
} );

// 封装axios的post请求
http.post = (url, params) => {
  return new Promise((resolve, reject) => {
    axios.post(url, params).then(response => {
        resolve(response.data);
      })
      .catch(err => {
        reject(err);
      });
  });
}



http.get = (url, params) => {
    return new Promise((resolve, reject) => {
      axios.get(url, params).then( response => {
        resolve(response.data)
      })
      .catch( err => {
        reject(err)
      })
    })
  }





export default http;